rm(list=ls())
set.seed(1234)
library(car)
library(plyr)
library(ggplot2)
library(psycho)
library(texreg)
library(psych)
library(effectsize)

#read in all original surveys 

setwd(".../Data/Raw")

Argentina<-read.csv("Argentina.csv")
AusNorFrance<-read.csv("AusNorFrance.csv")
Brazil <-read.csv("Brazil.csv")
Mexico <-read.csv("Mexico.csv")
Peru <-read.csv("Peru.csv")
Port <-read.csv("PortugalComplete.csv")
Spain <-read.csv("SpainComplete.csv")
UK <-read.csv("UKComplete.csv")
USA <-read.csv("USA.csv")

#recode quota knowledge variable to indicate those who answered that their quota does not have a quota policy and create merged dataset 

Argentina$QuotaNo<-recode(Argentina$countryquotas, " 'No' =1; else =0 ")
Argentina$Country<-rep("Argentina", nrow(Argentina))
Argentina<-as.data.frame(cbind(Argentina$QuotaNo, Argentina$Country))

Brazil$QuotaNo<-recode(Brazil$countryquotas, " 'Não' =1; else =0 ")
Brazil $Country<-rep("Brazil", nrow(Brazil))
Brazil <-as.data.frame(cbind(Brazil $QuotaNo, Brazil $Country))

Mexico $QuotaNo<-recode(Mexico $countryquotas, " 'No' =1; else =0 ")
Mexico $Country<-rep("Mexico", nrow(Mexico))
Mexico <-as.data.frame(cbind(Mexico $QuotaNo, Mexico $Country))

Peru $QuotaNo<-recode(Peru $quotaknowledge, " 'No' =1; else =0 ")
Peru $Country<-rep("Peru", nrow(Peru))
Peru <-as.data.frame(cbind(Peru $QuotaNo, Peru $Country))

Port $QuotaNo<-recode(Port $countryquotas, " 'NÌ£o' =1; else =0 ")
Port $Country<-rep("Portugal", nrow(Port))
Port <-as.data.frame(cbind(Port $QuotaNo, Port $Country))

AusNorFrance $QuotaNo<-recode(AusNorFrance $Q9I5, " 'No' =1; else =0 ") #country is: QCOUNTRY
AusNorFrance <-as.data.frame(cbind(AusNorFrance $QuotaNo, AusNorFrance $QCOUNTRY))

Spain $QuotaNo<-recode(Spain $countryquotas, " 'No'=1; else =0 ")
Spain $Country<-rep("Spain", nrow(Spain))
Spain <-as.data.frame(cbind(Spain $QuotaNo, Spain $Country))

UK $QuotaNo<-recode(UK $countryquotas, " 'No'=1; else =0 ")
UK $Country<-rep("UK", nrow(UK))
UK <-as.data.frame(cbind(UK $QuotaNo, UK $Country))

USA $QuotaNo<-recode(USA $countryquotas, " 'No'=1; else =0 ")
USA $Country<-rep("USA", nrow(USA))
USA <-as.data.frame(cbind(USA $QuotaNo, USA $Country))

know<-as.data.frame(rbind(Argentina, Brazil, Mexico, Peru, Port, AusNorFrance, Spain, UK, USA))

know$V1 <-as.numeric(as.character(know$V1))

know2<-as.data.frame(tapply(know$V1, know$V2, mean, na.rm = TRUE))

know2$country<-sort(unique(know$V2))

names(know2)<-c("no_quota", "Country")

know2$yes_quota<-1 - know2$no_quota #create variable indicating those responded that their country does have a quota policy 

#Add quota threshold; note this value we calcuated based on our knowledge of each case, see Table 1 of the manuscript 

know2$threshold <-recode(know2$Country, "
'Argentina' = 50; 
'Australia'  = 23;   
'Brazil'    = 30;
'France'    = 50;
'Mexico'    = 50; 
'Norway'  =    32; 
'Peru'  = 50; 
'Portugal'     = 33; 
'Spain'  = 40 ; 
 'UK'       = 16; 
 'USA' = 0")


cor.test(know2 $yes_quota, know2 $threshold) # 0.9370997

pdf("Figure1.pdf", width = 8, height = 5.5)  # Width and height are in inches

plot(know2 $ yes_quota ~ know2 $ threshold, xlab="Quota Threshold", ylab="Percentage of respondents reporting that there is a national quota policy", main="Percentage of respondents reporting that there is a national quota policy \n compared to the country's actual quota threshold", xlim=c(0,60), ylim=c(0.2,0.9))
text(know2 $threshold, know2 $yes_quota, labels= know2 $Country, cex=0.9, font=2, adj=0)
text(7, 0.85, "R = 0.94, p < 0.001", cex=1, col="black")

dev.off()

##SI Table SI.3

latex_table <-xtable(know2)

# Export to a .tex file
sink("tableSI3.tex")
print(latex_table, type = "latex")
sink()

